Stored Procedures [dbo].[asi_PostMonetaryData]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@postingDatachar(12)12
@batchKeyuniqueidentifier16
@itemKeyuniqueidentifier16
@userKeyuniqueidentifier16
@orgKeyuniqueidentifier16
@systemKeyuniqueidentifier16
@accessKeyuniqueidentifier16
SQL Script
CREATE                    PROCEDURE [dbo].[asi_PostMonetaryData]
@postingData as char(12),
@batchKey uniqueidentifier = null,
@itemKey uniqueidentifier = null,
@userKey uniqueidentifier = null,
@orgKey uniqueidentifier = null,
@systemKey uniqueidentifier = null,
@accessKey uniqueidentifier = null
AS
/*     This stored procedure will do the following:
   1. Create temporary tables containing:
    - Joined payment, payment lines for payments in the batch being posted
    - Joined invoice, invoice line, invoice distribution lines for invoices
      affected (via payment applications) by the payments being posted.
   2. If we are posting invoices, call a stored procedure to create payment schedules
      for those invoices.
   3. Call stored proc. to re-apply payments where they are not applied down
      to the invoice distribution line level.
   4. Call stored proc. to create general ledger entries (recognition of income,
      unearned income, cost of goods sold, inventory, etc.)
   
    Note: The stored procs in steps 2-4 read the #tmpInvoice and #tmpPayments tables
    being created here in the parent proc.
   
   11/30/04 - pjr - Added SalesLocation stuff
   06/24/05 - rrk - Removed separate call to apply payments to schedules.  This is now done
                    in asi_ReApplyPayments.  Also added FirstPaymentDueDate to invoice processing.
   01/09/06    - rrk - Updates for GL transaction viewer (DT 2448):
                    - added Description and batch numbers to #tmpInvoice
                    - added Description, final batch number, contact key to #tmpPayments                 
*/

IF EXISTS (select * from dbo.sysobjects where id = object_id('dbo.#tmpPayments'))
DROP TABLE dbo.#tmpPayments
IF EXISTS (select * from dbo.sysobjects where id = object_id('dbo.#tmpInvoice'))
DROP TABLE dbo.#tmpInvoice
CREATE TABLE #tmpInvoice
(InvoiceKey uniqueidentifier, InvoiceNumber nvarchar(50), AccountingMethodCode nchar(1),
FinancialEntityKey uniqueidentifier,  DistFinancialEntityKey uniqueidentifier, PaymentTermsKey uniqueidentifier, InvoiceDate datetime, BatchKey uniqueidentifier, InvoiceLineKey uniqueidentifier, OrderLineNumber int, InvoiceLineNumber int,
PayPriority int, InvoiceDistributionKey uniqueidentifier, ExtendedIncome decimal(18,4),
ExtendedIncomeRecognized decimal(18,4), IncomeGLAccountKey uniqueidentifier,
ARGLAccountKey uniqueidentifier, DeferredIncomeGLAccountKey uniqueidentifier,
AppliedTotal decimal(18,4), InvoiceTypeCode nvarchar(50), ApplyingCredit bit, QuantitySold decimal(18,4), ParentInvoiceLineKey uniqueidentifier,
UnitIncome decimal(18,4), DeferralTermsKey uniqueidentifier, SourceCodeKey uniqueidentifier, ResponseMediaCode nvarchar(20), PriceSheetKey uniqueidentifier,
GeneratesSalesHistoryFlag bit, ExtendedCost decimal(18,4), WarehouseKey uniqueidentifier, OrderNumber nvarchar(50) COLLATE database_default, OrderTypeKey uniqueidentifier,
OrderDate datetime, BillToContactKey uniqueidentifier, ShipToContactKey uniqueidentifier, SoldToContactKey uniqueidentifier,
ProductKey uniqueidentifier, UndiscountedExtendedPrice decimal(18,4), UomKey uniqueidentifier, CommissionPlanKey uniqueidentifier, SalesTeamGroupKey uniqueidentifier, ExtendedPrice decimal(18,4),
SalesLocationKey uniqueidentifier, IsPledge bit, FirstPaymentDueDate datetime, Description nvarchar(50), OriginatingBatchNumber nvarchar(50), FinalBatchNumber nvarchar(50))
/* Temporary payment data - pre-existing payments that are applied to  */
/* the invoice (or to the parent order)... */
CREATE TABLE #tmpPayments
(SrcPaymentKey uniqueidentifier, SrcInvoiceLineKey uniqueidentifier, Amount decimal(18,4), FinancialEntityKey uniqueidentifier,
MonetaryApplicationKey uniqueidentifier, AppliedAmount decimal(18,4),
OrderNumber nvarchar(50) COLLATE database_default, OrderLineNumber int, InvoiceKey uniqueidentifier,
InvoiceLineKey uniqueidentifier, InvoiceDistributionKey uniqueidentifier, SrcInvoiceKey uniqueidentifier, DiscountTaken decimal(18,4),
PaymentDate datetime, InvFinancialEntityKey uniqueidentifier, BatchKey uniqueidentifier, BatchLineStatusCode int, CurrencyVariance decimal(18,4),
SalesLocationKey uniqueidentifier, Description nvarchar(50), FinalBatchNumber nvarchar(50), ContactKey uniqueidentifier,
TransactionDate datetime, TransactionType int, ApplicationBatchKey uniqueidentifier, PaymentTypePriority int,
CashGLAccountKey uniqueidentifier)
IF UPPER(@postingData) = 'PAYMENTS'
BEGIN
    IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
    BEGIN
        INSERT INTO #tmpPayments
        SELECT  vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
            vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
            vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
            vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
            vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
            vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
            vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
            vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
        FROM vPostingPaymentData
        WHERE vPostingPaymentData.SrcPaymentKey = @itemKey
    END
    ELSE
    BEGIN
        INSERT INTO #tmpPayments
        SELECT  vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
            vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
            vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
            vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
            vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
            vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
            vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
            vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
        FROM vPostingPaymentData
        WHERE vPostingPaymentData.BatchKey = @batchKey
    END
    
    INSERT INTO #tmpInvoice
    SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
    FROM dbo.vPostingInvoiceData PID
    INNER JOIN #tmpPayments on #tmpPayments.OrderNumber = PID.OrderNumber
    WHERE #tmpPayments.InvoiceKey is null
    UNION
    SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
    FROM dbo.vPostingInvoiceData PID
    INNER JOIN #tmpPayments on #tmpPayments.InvoiceKey = PID.InvoiceKey
END
ELSE IF UPPER(@postingData) = 'INVOICES'
BEGIN
    IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
    BEGIN
        INSERT INTO #tmpInvoice
        SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
        FROM dbo.vPostingInvoiceData PID
        WHERE PID.InvoiceKey = @itemKey
    END
    ELSE
    BEGIN
        INSERT INTO #tmpInvoice
        SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
        FROM dbo.vPostingInvoiceData PID
        WHERE PID.BatchKey = @batchKey
    END
    /* Now - if we are posting any credit invoices that are applied to existing invoices, insert those
    invoices into #tmpInvoice as well, so the credit applications can be re-applied by the sp_ReApplyPayments stored proc.
    The 'ApplyingCredit' bit (being set to 1 below) will flag the gl posting proc to ignore these lines.
    */

    INSERT INTO #tmpInvoice
    SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 1, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
    FROM dbo.vPostingInvoiceData PID
    INNER JOIN MonetaryApplication ma ON PID.InvoiceKey = ma.InvoiceKey
    INNER JOIN #tmpInvoice ti on ti.InvoiceLineKey = ma.SrcInvoiceLineKey
    
    /* Temporary payment data - pre-existing payments that are applied to  */
    /* the invoice (or to the parent order)... */
    INSERT INTO #tmpPayments
    SELECT  PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
        PPD.MonetaryApplicationKey, PPD.AppliedAmount,
        PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
        PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
        PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
        PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
        PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
    FROM vPostingPaymentData PPD
    INNER JOIN #tmpInvoice on #tmpInvoice.OrderNumber = PPD.OrderNumber
    WHERE PPD.InvoiceKey is null
    UNION
    SELECT PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
        PPD.MonetaryApplicationKey, PPD.AppliedAmount,
        PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
        PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
        PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
        PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
        PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
    FROM dbo.vPostingPaymentData PPD
    INNER JOIN #tmpInvoice on #tmpInvoice.InvoiceKey = PPD.InvoiceKey
    WHERE PPD.SrcInvoiceLineKey is not null OR #tmpInvoice.ApplyingCredit = 0
    EXEC asi_CreatePaymentSchedules
END
/* Re-apply payments to the invoice distribution level for payments that are  */
/* currently applied to the order, order line, invoice or invoice line level... */
DECLARE @creditInvDistCreated bit
EXEC asi_ReApplyPayments @creditInvDistCreated output
IF UPPER(@postingData) = 'INVOICES' AND @creditInvDistCreated = 1
BEGIN
    /* If the re-apply payments stored proc created new credit invoice distribution lines, */
    /* we need to re-read the invoice data... */
    IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
    BEGIN
        INSERT INTO #tmpInvoice
        SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
        FROM dbo.vPostingInvoiceData PID
        WHERE PID.InvoiceKey = @itemKey
    END
    ELSE
    BEGIN
        INSERT INTO #tmpInvoice
        SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
        FROM dbo.vPostingInvoiceData PID
        WHERE PID.BatchKey = @batchKey
    END
    /* Now - if we are posting any credit invoices that are applied to existing invoices, insert those
    invoices into #tmpInvoice as well, so the credit applications can be re-applied by the sp_ReApplyPayments stored proc.
    The 'ApplyingCredit' bit (being set to 1 below) will flag the gl posting proc to ignore these lines.
    */

    INSERT INTO #tmpInvoice
    SELECT     DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
        PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
        PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber,  PID.InvoiceLineNumber,
        PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
        PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
        PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 1, PID.QuantitySold, PID.ParentInvoiceLineKey,
        PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
        PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
        PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
        PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
        PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
        PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
    FROM dbo.vPostingInvoiceData PID
    INNER JOIN MonetaryApplication ma ON PID.InvoiceKey = ma.InvoiceKey
    INNER JOIN #tmpInvoice ti on ti.InvoiceLineKey = ma.SrcInvoiceLineKey
END
/* Now re-populate tmpPayments with the newly re-applied payments, */
/* for use by the GL Data posting process... */
DELETE #tmpPayments
IF UPPER(@postingData) = 'PAYMENTS'
BEGIN
    IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
    BEGIN
        INSERT INTO #tmpPayments
        SELECT  vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
            vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
            vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
            vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
            vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
            vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
            vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
            vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
        FROM vPostingPaymentData
        WHERE vPostingPaymentData.SrcPaymentKey = @itemKey
    END
    ELSE
    BEGIN
        INSERT INTO #tmpPayments
        SELECT  vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
            vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
            vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
            vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
            vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
            vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
            vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
            vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
        FROM vPostingPaymentData
        WHERE vPostingPaymentData.BatchKey = @batchKey
    END
END
ELSE IF UPPER(@postingData) = 'INVOICES'
BEGIN
    INSERT INTO #tmpPayments
    SELECT PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
        PPD.MonetaryApplicationKey, PPD.AppliedAmount,
        PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
        PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
        PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
        PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
        PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
    FROM vPostingPaymentData PPD
    INNER JOIN #tmpInvoice on #tmpInvoice.InvoiceDistributionKey = PPD.InvoiceDistributionKey
END
EXEC asi_PostGLData @postingData, @userKey, @orgKey, @systemKey, @accessKey
DROP TABLE #tmpInvoice
DROP TABLE #tmpPayments
IF @batchKey is not null AND @batchKey != '00000000-0000-0000-0000-000000000000'
BEGIN
    UPDATE InvoiceMain set BatchLineStatusCode = 1
    WHERE FinalBatchKey = @batchKey
    UPDATE PaymentMain set BatchLineStatusCode = 1
    WHERE FinalBatchKey = @batchKey
END

GO
Uses